package com.anyou.mall.dri.service.impl;

import com.anyou.common.core.domain.ApiResult;
import com.anyou.common.utils.DateUtils;
import com.anyou.common.utils.uuid.RandomKit;
import com.anyou.mall.dri.domain.Team;
import com.anyou.mall.dri.domain.TeamAudit;
import com.anyou.mall.dri.mapper.TeamAuditMapper;
import com.anyou.mall.dri.mapper.TeamMapper;
import com.anyou.mall.dri.param.IsPassTeamAuditParam;
import com.anyou.mall.dri.service.ITeamAuditService;
import com.anyou.mall.platform.service.IPlaPlatformSettingsService;
import com.anyou.mall.use.domain.UserAuditNotice;
import com.anyou.mall.use.domain.UserNotice;
import com.anyou.mall.use.service.IUserAuditNoticeService;
import com.anyou.mall.use.service.IUserNoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 司机加盟团队申请信息表Service业务层处理
 *
 * @author gaochaohao
 * @date 2022-03-22
 */
@Service
public class TeamAuditServiceImpl extends ServiceImpl<TeamAuditMapper,TeamAudit> implements ITeamAuditService
{
    @Autowired
    private TeamAuditMapper teamAuditMapper;
    @Autowired
    private TeamMapper teamMapper;

    @Autowired
    private IUserAuditNoticeService userAuditNoticeService;

    @Autowired
    private IUserNoticeService userNoticeService;

    @Autowired
    private ITeamAuditService teamAuditService;

    @Autowired
    private IPlaPlatformSettingsService plaPlatformSettingsService;

    /**
     * 查询司机加盟团队申请信息表
     *
     * @param teamAuditId 司机加盟团队申请信息表主键
     * @return 司机加盟团队申请信息表
     */
    @Override
    public TeamAudit selectTeamAuditByTeamAuditId(Long teamAuditId)
    {
        return teamAuditMapper.selectTeamAuditByTeamAuditId(teamAuditId);
    }

    /**
     * 查询司机加盟团队申请信息表列表
     *
     * @param teamAudit 司机加盟团队申请信息表
     * @return 司机加盟团队申请信息表
     */
    @Override
    public List<TeamAudit> selectTeamAuditList(TeamAudit teamAudit)
    {
        return teamAuditMapper.selectTeamAuditList(teamAudit);
    }

    /**
     * 新增司机加盟团队申请信息表
     *
     * @param teamAudit 司机加盟团队申请信息表
     * @return 结果
     */
    @Override
    public int insertTeamAudit(TeamAudit teamAudit)
    {
        teamAudit.setCreateTime(DateUtils.getNowDate());
        return teamAuditMapper.insertTeamAudit(teamAudit);
    }

    /**
     * 修改司机加盟团队申请信息表
     *
     * @param teamAudit 司机加盟团队申请信息表
     * @return 结果
     */
    @Override
    public int updateTeamAudit(TeamAudit teamAudit)
    {
        teamAudit.setUpdateTime(DateUtils.getNowDate());
        return teamAuditMapper.updateTeamAudit(teamAudit);
    }

    /**
     * 批量删除司机加盟团队申请信息表
     *
     * @param teamAuditIds 需要删除的司机加盟团队申请信息表主键
     * @return 结果
     */
    @Override
    public int deleteTeamAuditByTeamAuditIds(Long[] teamAuditIds)
    {
        return teamAuditMapper.deleteTeamAuditByTeamAuditIds(teamAuditIds);
    }

    /**
     * 删除司机加盟团队申请信息表信息
     *
     * @param teamAuditId 司机加盟团队申请信息表主键
     * @return 结果
     */
    @Override
    public int deleteTeamAuditByTeamAuditId(Long teamAuditId)
    {
        return teamAuditMapper.deleteTeamAuditByTeamAuditId(teamAuditId);
    }

    @Override
    public ApiResult isPassTeamAudit(IsPassTeamAuditParam isPassTeamAuditParam) {
        TeamAudit teamAudit = teamAuditMapper.selectTeamAuditByTeamAuditId(isPassTeamAuditParam.getTeamAuditId());
        if (teamAudit != null) {
            if ("1".equals(isPassTeamAuditParam.getAuditStatus()) && !"1".equals(teamAudit.getAuditStatus())) {
                //通过
                //生成团队审核入住保证金订单号
                String orderId = RandomKit.genOrderId();
                teamAudit.setTeamBandOrderNo("tea" + orderId);
                teamAudit.setAuditStatus("1");
                int i = teamAuditMapper.updateTeamAudit(teamAudit);
                if (i > 0) {
                    Boolean payBaod = plaPlatformSettingsService.isPayBaod(3);
                    if (!payBaod) {
                        teamAudit.setBondStatus("1");
                        teamAudit.setPayBondTime(new Date());
                        teamAuditService.updateById(teamAudit);
                        //添加司机团队
                        Team team = new Team();
                        BeanUtils.copyProperties(teamAudit, team);
                        team.setTeamAuditId(teamAudit.getTeamAuditId());
                        teamMapper.insertTeam(team);
                        return ApiResult.success();
                    }
                }

            } else if ("2".equals(isPassTeamAuditParam.getAuditStatus())) {

                TeamAudit teamAudit1 = teamAuditService.getOne(new LambdaQueryWrapper<TeamAudit>().eq(TeamAudit::getTeamAuditId, isPassTeamAuditParam.getTeamAuditId()));
                teamAudit1.setAuditStatus("2");
                teamAudit1.setFailReason(isPassTeamAuditParam.getFailReason());
                return ApiResult.success(teamAuditMapper.updateTeamAudit(teamAudit1));
            } else {
                return ApiResult.error("审核失败");
            }
        }
        return ApiResult.error("审核失败");

    }

    @Override
    public ApiResult allPassTeamAudit(Long[] teamAuditIds) {
        if (teamAuditIds == null) {
            return ApiResult.error("请选择通过的审核");
        }
        for (Long teamAuditId : teamAuditIds) {
            TeamAudit teamAudit = teamAuditMapper.selectTeamAuditByTeamAuditId(teamAuditId);
            if (teamAudit != null && !"1".equals(teamAudit.getAuditStatus())) {

                //生成团队审核入住保证金订单号
                String orderId = RandomKit.genOrderId();
                teamAudit.setTeamBandOrderNo("tea" + orderId);
                teamAudit.setAuditStatus("1");
                int i = teamAuditMapper.updateTeamAudit(teamAudit);
                if (i > 0) {
                    Boolean payBaod = plaPlatformSettingsService.isPayBaod(3);
                    if (!payBaod) {
                        teamAudit.setBondStatus("1");
                        teamAudit.setPayBondTime(new Date());
                        teamAuditService.updateById(teamAudit);
                    }
                }
            }
        }
        return ApiResult.success();
    }

    @Override
    public List<TeamAudit> selectPassTeamAuditList() {
        List<TeamAudit> teamAudits = teamAuditMapper.selectPassTeamAuditList();
        return teamAudits;
    }
}
